
package com.skcc.cop.mrtaxi.biz.history;


import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.projectteam.http.util.DeviceUtil;
import com.skcc.cop.mrtaxi.BaseActivity;
import com.skcc.cop.mrtaxi.R;
import com.skcc.cop.mrtaxi.http.CallHistoryListListener;
import com.skcc.cop.mrtaxi.http.model.CallHistory;
import com.skcc.cop.mrtaxi.util.Dialogs;
import com.skcc.cop.mrtaxi.util.Dialogs.DialogListener;
import com.skcc.cop.mrtaxi.util.LogControl;
import com.skcc.cop.mrtaxi.view.TitleBar;
import com.skcc.cop.mrtaxi.view.TitleBar.OnTitleBarEventListener;


public class CallHistoryActivity extends BaseActivity implements OnTitleBarEventListener {
	
	// ===========================================================
    // Constants
    // ===========================================================
    private static final String TAG = CallHistoryActivity.class.getSimpleName();
    
    private TitleBar mTitleBarView;
    
    private ListView mCallHistoryList;
	private CallHistoryAdapter mAdapter;
	private List<CallHistory> historyList = new ArrayList<CallHistory>();
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_call_history);
		
		mTitleBarView = (TitleBar) this.findViewById(R.id.titlebar_view);
		mTitleBarView.setEventListener(this);
		mTitleBarView.showBack();
		mTitleBarView.setTitle(R.string.title);
		
		mCallHistoryList = (ListView) this.findViewById(R.id.history_calllist_listview);
		mCallHistoryList.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				
				CallHistory item = (CallHistory) historyList.get(arg2);
				
				LogControl.d(TAG, ""+item);
				/*
				Intent intent = new Intent(CallHistoryActivity.this, UserCommentActivity.class);
				startActivity(intent);*/
			}
			
		});
		
		mAdapter = new CallHistoryAdapter(CallHistoryActivity.this, R.layout.view_list_item_callhistory, historyList); 
		mCallHistoryList.setAdapter(mAdapter);
		
		// 콜 이력 조회 데이터 요청
		getCallHistory();	
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		LogControl.d(TAG, " in onResume >>>>>");
		
	}

	@Override
	protected void onPause() {
		super.onPause();
		
		LogControl.d(TAG, " in onPause >>>>>");
	}

	@Override
	protected void onDestroy() {
//		application.resetResource();
		
		LogControl.d(TAG, " in onDestroy >>>>>");
		
		super.onDestroy();
	}
	
	@Override
	public void onBackClicked() {
		finish();
	}
	
	@Override
	public void onAddClicked() {
		// TODO Auto-generated method stub
		
	}
	
	
	private void getCallHistory() {
		
		showProgressDialog(this, R.string.loading);
		
		DeviceUtil device = new DeviceUtil(this);
		gateway.getCallHistoryList(device.getIMEI(), new CallHistoryListListener() {

			@Override
			public void onNoNetwork() {
				if (!isFinishing()) {
					dismissProgressDialog();
				
					Dialogs.showDialog(CallHistoryActivity.this, getString(R.string.no_network), dialogListener);
				}
			}

			@Override
			public void onNoResponse() {
				dismissProgressDialog();
				
				Dialogs.showDialog(CallHistoryActivity.this, getString(R.string.no_respond), dialogListener);
			}

			@Override
			public void onSessionTimeOut() {
				dismissProgressDialog();
				
				Dialogs.showDialog(CallHistoryActivity.this, getString(R.string.session_timeout), dialogListener);
			}

			@Override
			public void onSuccess(List<CallHistory> callHistoryList) {
				dismissProgressDialog();
				
				historyList.clear();
				historyList.addAll(callHistoryList);
				
				mAdapter.notifyDataSetChanged();
			}

			@Override
			public void onFail(int code, String msg) {
				dismissProgressDialog();
				
				Dialogs.showDialog(CallHistoryActivity.this, 
						getString(R.string.server_message) + msg,
						dialogListener);
			}
			
		});
	}
	
	private DialogListener dialogListener = new DialogListener() {

		@Override
		public void onClickedOk() {
			Dialogs.dismiss();
		}

		@Override
		public void onClickedCancel() {
			// TODO Auto-generated method stub
			
		}
		
	};
	
	
	class CallHistoryAdapter extends ArrayAdapter<CallHistory> {
		private final LayoutInflater inflater;
		private final int resource;

		public CallHistoryAdapter(Context context, int resId, List<CallHistory> items)
		{
			super(context, resId, items);

			this.resource = resId;
			this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder;

			if(convertView == null)
			{
				convertView = inflater.inflate(resource, parent, false);

				holder = new ViewHolder();
				holder.date = (TextView) convertView.findViewById(R.id.date);
				holder.carNumber = (TextView) convertView.findViewById(R.id.carNumber);
				holder.usingLog = (TextView) convertView.findViewById(R.id.usingLog);
				holder.postscript = (TextView) convertView.findViewById(R.id.postscript);
				holder.rating = (TextView) convertView.findViewById(R.id.rating);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			CallHistory item = getItem(position);
			holder.date.setText(item.getInptDt());
			holder.carNumber.setText(item.getCarNumber());
			holder.usingLog.setText(item.getUsingLog());
			holder.postscript.setText(item.getPostscript());
			holder.rating.setText(String.valueOf(item.getRating()));

			return convertView;
		}

		class ViewHolder
		{
			TextView date;
			TextView carNumber;
			TextView usingLog;
			TextView postscript;
			TextView rating;
		}
	}

	

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.call_history, menu);
//		return true;
//	}
}
